/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "i2c.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ssd1306.h"
#include "chinese_font.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM2_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
  
  /* 等待系统稳定 */
  HAL_Delay(1000);
  /* 简单的I2C连接测试 */
  uint8_t i2c_test_passed = 0;
  
  // 测试0x3C地址
  for (int i = 0; i < 10; i++) {
    if (HAL_I2C_IsDeviceReady(&hi2c1, 0x3C << 1, 3, 100) == HAL_OK) {
      i2c_test_passed = 1;
      break;
    }
    HAL_Delay(50);
  }
  
  // 如果0x3C失败，测试0x3D
  if (!i2c_test_passed) {
    for (int i = 0; i < 10; i++) {
      if (HAL_I2C_IsDeviceReady(&hi2c1, 0x3D << 1, 3, 100) == HAL_OK) {
        i2c_test_passed = 1;
        break;
      }
      HAL_Delay(50);
    }
  }
  
  if (!i2c_test_passed) {
    // I2C连接失败，LED快闪
    while(1) {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
      HAL_Delay(100);
    }
  }
  
  /* 初始化OLED显示屏 */
  if (SSD1306_Init(&hi2c1) != 0) {
    // OLED初始化失败，LED慢闪表示错误
    while(1) {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
      HAL_Delay(500);
    }
  }
  

  SSD1306_Clear();
  SSD1306_SetCursor(0, 0);  // 设置起始位置
  const uint8_t custom_char_8x16[16][16] = {
    {0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//_
    {0x00,0x00,0x10,0x10,0x3F,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00},//1
    /*-- ID:0,字符:"2",ASCII编码:32,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x1C,0x20,0x20,0x20,0x21,0x1E,0x00,0x00,0x18,0x28,0x48,0x88,0x08,0x18,0x00},

/*-- ID:1,字符:"3",ASCII编码:33,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00},

/*-- ID:2,字符:"4",ASCII编码:34,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x00,0x03,0x04,0x18,0x3F,0x00,0x00,0x00,0xC0,0x40,0x48,0x48,0xF8,0x48,0x48},

/*-- ID:3,字符:"5",ASCII编码:35,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x3F,0x22,0x22,0x22,0x21,0x20,0x00,0x00,0x30,0x08,0x08,0x08,0x10,0xE0,0x00},

/*-- ID:4,字符:"6",ASCII编码:36,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x0F,0x11,0x22,0x22,0x12,0x01,0x00,0x00,0xE0,0x10,0x08,0x08,0x08,0xF0,0x00},

/*-- ID:5,字符:"7",ASCII编码:37,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x30,0x20,0x20,0x23,0x2C,0x30,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00},

/*-- ID:6,字符:"8",ASCII编码:38,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00},

/*-- ID:7,字符:"9",ASCII编码:39,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
{0x00,0x1F,0x20,0x20,0x20,0x11,0x0F,0x00,0x00,0x00,0x90,0x88,0x88,0x10,0xE0,0x00},



};
  // “你”字16×16点阵数据（ID:0，ASCII:C4E3，32字节）
  const uint8_t hanzi[32][32] = {
    /*-- ID:0,字符:"烟",ASCII编码:D1CC,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    {
    		0x88,0x40,0x98,0xff,0x98,0x61,0xd8,0x65,0xfa,0x6d,0xbe,0x6d,0x9e,0x7f,0x9a,0x6d,
    		0x98,0x6d,0x98,0x75,0xb8,0x73,0xec,0x73,0xe4,0x61,0xa4,0x61,0x82,0x7f,0x80,0x20



    },

    /*-- ID:1,字符:"台",ASCII编码:CCA8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    {0x00,0x01,0x80,0x03,0xc0,0x08,0x60,0x30,0x30,0x60,0xfc,0xff,0x18,0x40,0x10,0x20,
    0xf0,0x7f,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0xf0,0x3f,0x10,0x10},

    /*-- ID:2,字符:"大",ASCII编码:B4F3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    {0x80,0x00,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x21,0xfc,0x7f,0x80,0x01,0x80,0x03,
    0xc0,0x02,0xc0,0x06,0x60,0x04,0x60,0x0c,0x30,0x18,0x18,0x70,0x0c,0xe0,0x02,0x40},

    /*-- ID:3,字符:"学",ASCII编码:D1A7,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    {0x90,0x08,0xb0,0x19,0x28,0x4d,0xfc,0xff,0x0e,0x60,0x04,0x24,0xe0,0x0f,0x00,0x06,
    0x00,0x03,0x00,0x23,0xfc,0x7f,0x00,0x03,0x00,0x03,0x00,0x03,0xc0,0x03,0x00,0x01},
};

   // SSD1306_ShowCustomChar(2, 10,custom_char_8x16[1],0);
  // 空心
  // SSD1306_DrawHeart(64, 32, 2);
  // 屏幕中心（64,32）绘制半径10的标准心形（最协调大小）
//   SSD1306_DrawStandardHeart(64, 32, 25);

  // // 假设汉字显示逆时针偏90度，用rotate=1（顺时针旋转90度）校正
  // SSD1306_ShowChinese16x16_Rotate(56, 24, hanzi, 2, 1);
  SSD1306_ShowChinese16x16(20, 24, hanzi[0], 0);
  SSD1306_ShowChinese16x16(40, 24, hanzi[1], 0);
  SSD1306_ShowChinese16x16(60, 24,hanzi[2], 0);
  SSD1306_ShowChinese16x16(80, 24, hanzi[3], 0);

  SSD1306_UpdateScreen();

  HAL_Delay(500);
 SSD1306_HorizontalScroll(0, 0, 7, 0);
  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */




//	  SSD1306_HeartBeatAnimation(64, 32, 20, 1);
    /* USER CODE BEGIN 3 */
    /* 主循环中不需要额外操作 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);  /* 错误时闪烁LED */
    HAL_Delay(100);
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

